package com.ray.tcc.shiro;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.RpcException;
import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.emun.ManagerType;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Menu;
import com.ray.auth.system.table.entity.User;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.error.MessageHandle;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.shiro.db.RealmDataBaseRead;
import com.zjhc.shiro.entity.ShiroUser;

/**
 * <h1>自定义读取数据库信息 </h1>
 * @创建人 ray
 * @创建时间 2017-06-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
public class HongWebRealm implements RealmDataBaseRead{
	private static final Logger LOGGER = Logger.getLogger(HongWebRealm.class);
    @Autowired
    private UcenterApiService  ucenterApiService;
	
	
	
	public ShiroUser queryUser(String userName) {
		ShiroUser shiroUser = null;
		try {
			if(StrUtil.isNotBlank(userName)){
				//获取用户信息
				Result<User> userResult = ucenterApiService.queryUser(userName);
				if(userResult.codeSuccess()){
					User user = userResult.getData();
					if(ObjectUtil.isNotNull(user)){
						shiroUser = new ShiroUser();
						shiroUser.setUserName(userName);
						shiroUser.setPassword(user.getPassword());
						shiroUser.setExpendValue(user);
					}
				}else{
					throw new BussinessException(userResult.getCode(), userResult.getMsg());
				}
				
			}
		} catch (RpcException e) {//Rpc异常
			LOGGER.error("RPC调用异常");
		}catch (Exception e) {
			LOGGER.error("内部异常");
		}
		return shiroUser;
	}

	
	public Set<String> queryResours(String userName) {
		//放置请求路径 及页面路径
		Set<String> set = null;
		try {
			if(StrUtil.isNotBlank(userName)){
				//获取用户信息
				Result<User> userResult = ucenterApiService.queryUser(userName);
				if(userResult.codeSuccess()){
					User user = userResult.getData();
					if(ObjectUtil.isNotNull(user)){
						//应用id
						String appid = MessageHandle.getMsgByKey("app.id");
						set = new HashSet<String>();
						//超级管理员默认拥有所有权限
						if(StrUtil.equals(user.getManagerType(), ManagerType.SUPER.getType())){
							
							Result<List<Menu>> menuResult = ucenterApiService.querySuperUserMenus(appid);
							if(menuResult.codeSuccess()){
								//菜单路径
								for(Menu menu : menuResult.getData()){
									//放置用户请求路径
									set.add(menu.getAuthUrl());
								}
							}else{
								throw new BussinessException(userResult.getCode(), userResult.getMsg());
							}
							
							Result<List<Button>> buttonResult = ucenterApiService.querySuperUserButtons(appid);
							if(buttonResult.codeSuccess()){
								//按钮路径
								for(Button button :buttonResult.getData()){
									//放置用户请求路径
									set.add(button.getAuthUrl());
								}
							}else{
								throw new BussinessException(userResult.getCode(), userResult.getMsg());
							}
							
						
						}else{
							
							Result<List<Menu>> menuResult = ucenterApiService.queryUserMenus(user.getUserId(),appid);
							if(menuResult.codeSuccess()){
								//菜单路径
								for(Menu menu : menuResult.getData()){
									//放置用户请求路径
									set.add(menu.getAuthUrl());
								}
							}else{
								throw new BussinessException(userResult.getCode(), userResult.getMsg());
							}
							
							Result<List<Button>> buttonResult = ucenterApiService.queryUserButtons(user.getUserId(),appid);
							if(buttonResult.codeSuccess()){
								//按钮路径
								for(Button button :buttonResult.getData()){
									//放置用户请求路径
									set.add(button.getAuthUrl());
								}
							}else{
								throw new BussinessException(userResult.getCode(), userResult.getMsg());
							}

						}
					}
				}else{
					throw new BussinessException(userResult.getCode(), userResult.getMsg());
				}
				
			}
		} catch (RpcException e) {//Rpc异常
			LOGGER.error("RPC调用异常");
		}catch (Exception e) {
			LOGGER.error("内部异常");
		}
		return set;
	}

}
